import {ajax,silentAfter,defaultAfter,defaultBefore} from "@/utils/ajax.js" 

export const loginOrRegister = async ({mobile,verCode})=>{
	if(loginOrRegister.lock){
		throw new Error('正在请求中')
	}
	try{
		loginOrRegister.lock=true;
		const res = await ajax({
			url:'/login/loginOrRegister',
			data:{
				mobile,
				verCode
			}
		})
		return res;
	}catch(e){
		throw e
	}finally{
		loginOrRegister.lock = false;
	}
}
export const realLoginByUserNum = async ({ userNum, pwd, verCode, verToken }) => {
    if (realLoginByUserNum.lock) {
        throw new Error('正在请求中，请勿重复提交');
    }

    try {
        realLoginByUserNum.lock = true;

        const res = await ajax({
            url: '/login/realLoginByUserNum',
    
            data: {
                userNum,
                pwd,
                verCode,
                verToken
            }
        });

        return res;
    } catch (e) {
        console.error('登录失败:', e);
        uni.showToast({ title: '登录失败，请重试', icon: 'none' });
        throw e;
    } finally {
        realLoginByUserNum.lock = false;
    }
};


export const realRegByUserNum = async ({mobile,verCode})=>{
	if(loginOrRegister.lock){
		throw new Error('正在请求中')
	}
	try{
		loginOrRegister.lock=true;
		const res = await ajax({
			url:'/login/realRegByUserNum',
			data:{
				mobile,
				verCode
			}
		})
		return res;
	}catch(e){
		throw e
	}finally{
		loginOrRegister.lock = false;
	}
}


export const logout = () => {
  return ajax(
    {
      url: "/login/quit"
    },
    defaultBefore,
    ({ data }) => {
		console.log(data)
      // 保证 data 存在
      if (!data) {
        throw new Error('登出接口返回空数据');
      }

      const { retCode, retDesc } = data;
	  console.log(retCode, retDesc )

      // // 判断是否成功
      if (retCode === '000000') {
        return data.data || true;
      }

      // 失败时抛出明确错误
      const errorMsg = retDesc || '登出失败，请稍后再试';
      throw new Error(errorMsg);
    }
  );
};
export const getImageCode = ()=>{
	return ajax({
		url: "/login/getImageCode"
	})
}

export const mustCaptcha = ({mobile,type='login'}) =>{
	return ajax({
		url: "/sms/isVrCode",
		data:{
			mobile,
			type
		}
	})
}

export const sendMobileCode=({mobile,type='login',imageCode=1357,imageToken})=>{
	return ajax({
		url:'/login/sendMobileCode',
		data:{
			mobile,
			type,
			imageCode,
			imageToken
		}
	})
}

export const sendSms = ({mobile,type='login',imgCode,imgToken},header)=>{
	return ajax({
		url:'/sms/sendSms',
		header,
		data:{
			mobile,
			type,
			imgCode,
			imgToken
		}
	})
}


export const getUserInfo = ()=>{
	return ajax({
		url:'/user/getUserInfo'
	})
}

export const getPortalUserInfo = (id)=>{
	return ajax({
		url:'/user/detail',
		data:{
			id
		}
	},defaultBefore,silentAfter)
}


export const wxAppLogin = async (code)=>{
	if(wxAppLogin.lock){
		throw new Error('正在请求中')
	}
	try{
		wxAppLogin.lock=true;
		const res = await ajax({
			url:'/login/wxAppLogin',
			data:{
				code
			}
		})
		return res;
	}catch(e){
		throw e
	}finally{
		wxAppLogin.lock = false;
	}
}


export const reportDevice = ({pushId,brand,platform,model,system,event,deviceId}) =>{
	if(process.env.NODE_ENV == 'development'){
		return true
	}
	return ajax({
		url:'/user/reportDevice',
		data:{
			pushId,
			brand,
			platform,
			model,
			system,
			// #ifdef APP-PLUS
			channel:'APP',
			// #endif
			// #ifdef MP-WEIXIN
			channel:'wxMini',
			// #endif
			// #ifdef H5
			channel:'H5',
			// #endif
			event,
			deviceId
			
		}
	})
}

export const bindMobile = async ({mobile,verCode},header) =>{
	if(bindMobile.lock){
		throw new Error('正在请求中')
	}
	try{
		bindMobile.lock=true;
		const res = await ajax({
			url:'/user/bindMobile',
			header,
			data:{
				mobile,
				verCode
			}
		})
		return res;
	}finally{
		bindMobile.lock = false;
	}
}

export const searchUsers = ({key,pageNo=1,pageSize=10})=>{
	return ajax({
		url:'/user/search',
		data:{
			keyWords:key,
			pageNo,
			pageSize
		}
	})
}

export const modifyUser =({userName,userNum,sex,avatarUrl,profiles,province,city},header)=>{
	return ajax({
		url:'/user/update',
		header,
		data:{
			userNum,
			userName,
			sex,
			avatarUrl,
			profiles,
			province,
			city,
		}
	})
}

export const refreshUserInfo = () =>{
	return ajax({
		url:'/user/refreshUserInfo'
	})
}

export const setMyBgImg=(bgImg)=>{
	return ajax({
		url:'/userExt/upBgImg',
		data:{
		  	bgImg
		}
	})
}


export const quickLogin = ({openid,accessToken})=>{
	return ajax({
		url:'/login/phoneFast',
		data:{
			openid,
			accessToken
		}
	})
}

export const getRealResult = () =>{
	return ajax({
		url:'/authInfo/getRealName',
		data:{}
	})
}


export const accountRegister=({userNum,pwd,verCode,verToken})=>{
	return ajax({
		url:'/login/userNumRegister',
		data:{
			userNum,
			pwd,
			verCode,
			verToken
		}
	})
}

export const zhuce=({userNum,pwd,verCode,verToken})=>{
	return ajax({
		url:'/login/realRegByUserNum',
		data:{
			userNum,
			pwd,
			verCode,
			verToken
		}
	})
}
export const accountLogin=({
	userNum,
	pwd,
	verCode,
	verToken
})=>{
	return ajax({
		url:'/login/userNumLogin',
		data:{
			userNum,
			pwd,
			verCode,
			verToken
		}
	})
}


export const checkQrcode = (id)=>{
	return ajax({
		url:'/scanCode/scanQrCodeImg',
		data:{
			id
		}
	})
}

export const handleQrcode = (id,type)=>{
	return ajax({
		url:'/scanCode/confirmLogin',
		data:{
			id,
			operationType:type
		}
	})
}

export const resetPassword=({password},header)=>{
	return ajax({
		url:'/user/resetPassword',
		header,
		data:{
			password
		}
	})
}

export const setIsSearch = (state)=>{
	return ajax({
		url:'/userExt/upIsSearch',
		data:{
			state
		}
	})
}

export const setIsAddValidate = (state)=>{
	return ajax({
		url:'/userExt/upIsAddValidate',
		data:{
			state
		}
	})
}